package javango.contrib.admin.api;

import java.util.Iterator;

import javango.contrib.admin.ModelManager;
import javango.contrib.hibernate.HibernateUtil;
import javango.contrib.hibernate.ModelDao;

import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;

import com.google.inject.Inject;

/**
 * Default implementation of admin options tha allows all known hibernate entities to be managed.  
 * 
 * @author johns
 *
 */
public class DefaultAdminOptions implements AdminOptions {

	HibernateUtil hibernateUtil;
	
	@Inject
	public DefaultAdminOptions(HibernateUtil hibernateUtil) {
		this.hibernateUtil = hibernateUtil;
	}
	
	/**
	 * Return an iterator of all available class mappings
	 * @return
	 */
	
	public Iterator<PersistentClass> getClassMappings() {
		Configuration cfg = hibernateUtil.getConfiguration();
		return cfg.getClassMappings();
	}
	
	/**
	 * Return the class mapping for the specified class
	 * @return
	 */
	public PersistentClass getClassMapping(String entityName) {
		Configuration cfg = hibernateUtil.getConfiguration();
		return cfg.getClassMapping(entityName);
	}
	
	public ModelManager<?> getManager(Class entityClass) {
		return new javango.contrib.admin.BaseModelManager(entityClass, new ModelDao(hibernateUtil, entityClass));
	}
}
